package com.tieshan.smartwindow.ui

import android.content.pm.PackageManager
import android.view.View
import com.hjq.toast.ToastUtils
import com.tencent.mmkv.MMKV
import com.tieshan.smartwindow.R
import com.tieshan.smartwindow.app.AppActivity
import com.tieshan.smartwindow.other.FileConfig
import com.tieshan.smartwindow.other.FileUtil
import com.tieshan.smartwindow.other.MMKVConfig
import kotlinx.android.synthetic.main.deal_activitity.*
import timber.log.Timber

class DealActivitity : AppActivity() {

    private val mmkv: MMKV = MMKV.mmkvWithID(MMKVConfig.LiveLog)
    private val openDog: MMKV = MMKV.mmkvWithID(MMKVConfig.openDog)
    var sb = StringBuilder()
    fun init() {
        tv_verionCode.text="版本号：${getAppVersion()}"
        iv_home.setOnClickListener { finish() }
        tv_setting.setOnClickListener {
            val packageManager: PackageManager = this.getPackageManager()
            val intent = packageManager.getLaunchIntentForPackage("com.android.settings")
            this.startActivity(intent!!)
            sb = StringBuilder()
        }
        tv_rkfile.setOnClickListener {
            val packageManager: PackageManager = this.getPackageManager()
            val intent = packageManager.getLaunchIntentForPackage("com.android.rk")
            this.startActivity(intent!!)
            sb = StringBuilder()
        }
//        tv_open.setOnClickListener {
//            startActivity(USBActivity::class.java)
//        }
        tv_log.setOnClickListener {
            MMKV.mmkvWithID(MMKVConfig.LiveContent).putString(MMKVConfig.LiveContent, "")
            var log = mmkv.getBoolean(MMKVConfig.LiveLog, false)
            log = !log
            mmkv.putBoolean(MMKVConfig.LiveLog, log)
            if (log) {
                tv_log.setTextColor(this.resources.getColor(R.color.red))
                tv_log.text = "已打开心跳日志，点击关闭心跳日志,并将清除日志文件"
            }else {
                tv_log.setTextColor(this.resources.getColor(R.color.white))
                tv_log.text = "已关闭心跳日志，点击打开心跳日志记录"
                FileUtil.delAllFile(FileConfig.LiveLogPath)
            }
        }
        tv_restart.setOnClickListener {
            android.os.Process.killProcess(android.os.Process.myPid())
            System.exit(1)
        }

        tv_openDog.setOnClickListener {

            var log = openDog.getBoolean(MMKVConfig.openDog, true)
            log = !log
            openDog.putBoolean(MMKVConfig.openDog, log)
            if (log) {
                tv_openDog.setTextColor(this.resources.getColor(R.color.red))
                tv_openDog.text = "已打开看门狗，点击关闭看门狗"
            }else {
                tv_openDog.setTextColor(this.resources.getColor(R.color.white))
                tv_openDog.text = "已关闭看门狗，点击打开看门狗"
            }
        }
    }


    override fun getLayoutId(): Int {
        return R.layout.deal_activitity
    }

    override fun initView() {
        ll_menu.visibility=View.GONE
        ll_pwd.visibility=View.VISIBLE
               val log = mmkv.getBoolean(MMKVConfig.LiveLog, false)
        if (log) {
            tv_log.setTextColor(this.resources.getColor(R.color.red))
            tv_log.text = "已打开心跳日志，点击关闭心跳日志,并将清除日志文件"
        }else {
            tv_log.setTextColor(this.resources.getColor(R.color.white))
            tv_log.text = "已关闭心跳日志，点击打开心跳日志记录"
        }
        val openDog = openDog.getBoolean(MMKVConfig.openDog, true)
        if (openDog) {
            tv_openDog.setTextColor(this.resources.getColor(R.color.red))
            tv_openDog.text = "已打开看门狗，点击关闭看门狗"
        }else {
            tv_openDog.setTextColor(this.resources.getColor(R.color.white))
            tv_openDog.text = "已关闭看门狗，点击打开看门狗"
        }
        bt_ensure.setOnClickListener {
                    var pwd = ed_pwd.text.toString()
                    Timber.e(pwd)
                    if (pwd.equals("CFR123456")) {
                        ll_pwd.visibility = View.GONE
                        ll_menu.visibility = View.VISIBLE
                    } else {
                        ToastUtils.show("密码输入错误")
                        ed_pwd.setText("")
                    }
                    hideKeyboard(ed_pwd)
                }
                bt_ext.setOnClickListener { finish() }
                init()

    }

    // 获取应用版本信息
    private fun getAppVersion(): String {
        var   versionCode=0
        try {
            // 获取包管理器
            val packageManager = packageManager

            // 获取当前应用的包名
            val packageName = packageName

            // 通过包名获取包信息
            val packageInfo = packageManager.getPackageInfo(packageName, 0)

            // 获取版本名称 (例如：1.0)
            versionCode = packageInfo.versionCode

        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
            Timber.e(e.toString())
        }
        return versionCode.toString()
    }

    override fun initData() {
    }
}
